//
//  RecommendViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RecommendViewController.h"
#import "GlobalViewController.h"
#import "FeatureViewController.h"
#import "OfficialViewController.h"

@interface RecommendViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong)OfficialViewController *officialVC;
@property (nonatomic, strong)FeatureViewController *featureVC;
@property (nonatomic, strong)GlobalViewController *globalVC;
@property (nonatomic, assign)NSInteger currentPage;
@property (nonatomic, assign)NSInteger lastPage;
@property (nonatomic, assign)CGFloat lastContentOff_x;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, assign)BOOL isClick;
@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBar.translucent = NO;
    [self createChildVC];
    [self createView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"======11");
}

- (void)createView {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 64, WIDTH / 3, 50);
    [button setTitle:@"官方榜" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.view addSubview:button];
    button.tag = 15000;
    [button addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *featureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    featureButton.frame = CGRectMake(WIDTH / 3, 64, WIDTH / 3, 50);
    [featureButton setTitle:@"特色榜" forState:UIControlStateNormal];
    [featureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:featureButton];
    featureButton.tag = 15001;
    [featureButton addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *globalButton = [UIButton buttonWithType:UIButtonTypeCustom];
    globalButton.frame = CGRectMake(WIDTH / 3 * 2, 64, WIDTH / 3, 50);
    [globalButton setTitle:@"全球榜" forState:UIControlStateNormal];
    [globalButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:globalButton];
    globalButton.tag = 15002;
    [globalButton addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.array = [NSMutableArray arrayWithObjects:button, featureButton, globalButton, nil];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64 + 50, WIDTH, 2)];
    view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view];
    UIView *indicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, 2)];
    indicatorView.backgroundColor = [UIColor magentaColor];
    [view addSubview:indicatorView];
    indicatorView.tag = 15011;
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 52 + 64, WIDTH, HEIGHT - 52)];
    [self.view addSubview:scrollView];
    scrollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    [scrollView addSubview:self.officialVC.view];
    self.officialVC.view.frame = scrollView.bounds;
    scrollView.tag = 15100;
    self.officialVC.view.backgroundColor = [UIColor grayColor];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
}

- (void)BtnAction:(UIButton *)button {
    //tag值 15000 15001 15002
    UIScrollView *scrollView = [self.view viewWithTag:15100];
    self.currentPage = button.tag - 15000;
    self.isClick = YES;
    [scrollView setContentOffset:CGPointMake(WIDTH * (button.tag - 15000), 0) animated:YES];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    for (UIButton *button1 in self.array) {
        if (button1.tag != button.tag) {
            [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    [self scrollViewWillBeginDecelerating:scrollView];
    [self scrollViewDidEndDecelerating:scrollView];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.tag == 15100) {
        if (!self.isClick) {
            if (scrollView.contentOffset.x > self.lastContentOff_x) {
                self.currentPage = (int)scrollView.contentOffset.x / WIDTH;
                if (self.currentPage > 2) {
                    self.currentPage = 0;
                }
            } else if(scrollView.contentOffset.x < self.lastContentOff_x) {
                self.currentPage = (int)scrollView.contentOffset.x / WIDTH;
                if (self.currentPage < 0) {
                    self.currentPage = 2;
                }
            }
        }
        [self animationIndicatorViewWithDistance:(scrollView.contentOffset.x - self.lastContentOff_x) / 3];
        self.lastContentOff_x = scrollView.contentOffset.x;
    }
    
    
}

- (void)createChildVC {
    self.officialVC = [OfficialViewController new];
    self.featureVC = [FeatureViewController new];
    self.globalVC = [GlobalViewController new];
    [self addChildViewController:self.officialVC];
    [self addChildViewController:self.featureVC];
    [self addChildViewController:self.globalVC];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if (scrollView.tag == 15100) {
        self.isClick = NO;
    }
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // scrollView.tag = 15100
    // indicatorView.tag = 15011
    if (scrollView.tag == 15100) {
        for (int i = 0; i < 3; i++) {
            if (i == self.currentPage ) {
                if ([[self.childViewControllers[i] view] isDescendantOfView:scrollView]) {
                    UIButton *button = [self.view viewWithTag:15000 + self.currentPage];
                    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                    for (UIButton *button1 in self.array) {
                        if (button1.tag != button.tag) {
                            [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                        }
                    }
                    return;
                } else {
                    [scrollView addSubview:[self.childViewControllers[i] view]];
                    UIView *view = [self.childViewControllers[i] view];
                    view.frame = CGRectMake(WIDTH * i, 0, WIDTH, scrollView.frame.size.height);
                    UIButton *button = [self.view viewWithTag:15000 + self.currentPage];
                    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                    for (UIButton *button1 in self.array) {
                        if (button1.tag != button.tag) {
                            [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                        }
                    }
                }
            }
        }
    }
}

- (void)animationIndicatorViewWithDistance:(CGFloat) distance{
    [UIView animateWithDuration:0.2 animations:^{
        UIView *view = [self.view viewWithTag:15011];
        view.frame = CGRectMake(view.frame.origin.x + distance, 0, WIDTH / 3, 2);
        
    } completion:^(BOOL finished) {
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
